{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.5. Mastering IPython's configuration system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing random_magics.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile random_magics.py\n",
    "\n",
    "from traitlets import Int, Float, Unicode, Bool\n",
    "from IPython.core.magic import (Magics, magics_class,\n",
    "                                line_magic)\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Appending to random_magics.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile random_magics.py -a\n",
    "\n",
    "@magics_class\n",
    "class RandomMagics(Magics):\n",
    "    text = Unicode(u'{n}', config=True)\n",
    "    max = Int(1000, config=True)\n",
    "    seed = Int(0, config=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Appending to random_magics.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile random_magics.py -a\n",
    "\n",
    "    def __init__(self, shell):\n",
    "        super(RandomMagics, self).__init__(shell)\n",
    "        self._rng = np.random.RandomState(\n",
    "            self.seed or None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Appending to random_magics.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile random_magics.py -a\n",
    "\n",
    "    @line_magic\n",
    "    def random(self, line):\n",
    "        return self.text.format(\n",
    "            n=self._rng.randint(self.max))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Appending to random_magics.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile random_magics.py -a\n",
    "\n",
    "def load_ipython_extension(ipython):\n",
    "    ipython.register_magics(RandomMagics)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext random_magics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'430'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'305'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%random"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "ipython --RandomMagics.text='Your number is {n}.' \\\n",
    "        --RandomMagics.max=10 \\\n",
    "        --RandomMagics.seed=1\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "c.RandomMagics.text = 'random {n}'\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "from traitlets.config import Configurable\n",
    "from traitlets import Float\n",
    "\n",
    "class MyConfigurable(Configurable):\n",
    "    myvariable = Float(100.0, config=True)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "c = get_config()\n",
    "c.MyConfigurable.myvariable = 123.\n",
    "```"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
